#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      chmiboe
#
# Created:     26-08-2010
# Copyright:   (c) chmiboe 2010
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

def kb_while():
    i = 10
    while (i>0):
        print i
        i -= 1

def kb_break():
    import random as rnd
    while (True):
       val = rnd.randint(1, 30)
       print val,                       # , omits newlilne
       if (val ==  22):
          break

def kb_continue():
    num = 0

    while (num < 100):
       num = num + 1
       if (num % 2) == 0:
          continue
       print num,

def kb_ifelif():
    name = "Luke"

    if name == "Jack":
       print "Hello Jack!"
    elif name == "John":
       print "Hello John!"
    elif name == "Luke":
       print "Hello Luke!"
    else:
       print "Hello there!"

def kb_for():
    lyrics = """\
    the quick brown fox
    jumps over the lazy dog
    01234567890
    """
    for i in lyrics:
        print i,

def kb_boolInUsw():
    grades = ["A", "B", "C", "D", "E", "F"]

    grade = "L"

    if grade not in grades:
        print "unknown grade"

def test_module():
    from math import sin, pi

    print sin(pi/2)

def kb_lambda():
    for i in (1, 2, 3, 4, 5):
        a =  lambda x: x * x
        print a(i),

x = 15
def kb_global():
    #global x
    x = 45
    print x

def kb_exceptionTest():
    f = None
    try:
        f = open('test.txt', 'r')
        for i in f:
            print i,
    except IOError:
        print "error reading file"
    finally:
        if f:
            f.close()
    pass


class YesNoException(Exception):
   def __init__(self):
      print 'Invalid value'

def testOwnException():
    answer = 'y'

    if (answer != 'yes' and answer != 'no'):
       raise YesNoException
    else:
       print 'Correct value'

def kb_delete():
    a = [1, 2, 3, 4]
    print a
    del a[:2]
    print a

def kb_assert():
    salary = 3500
    salary -= 3560 # a mistake was done
    assert salary > 0

class Square:
   def __init__(self, x):
      self.a = x

   def area(self):
      print self.a * self.a

def kb_class():
    sq = Square(12)
    sq.area()

def kb_exec():
    exec("for i in [1, 2, 3, 4, 5]: print i,")

def kb_in():
    print 4 in (2, 3, 5, 6)

    for i in range(25):
        print i,

def kb_yield():
    def gen():
       x = 11
       yield x

    it = gen()
    print it.next()

kb_yield()

